package io.sunny.platform.mcp.controller;

import io.sunny.platform.mcp.domain.model.ChatRequest;
import io.sunny.platform.mcp.domain.model.ChatResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Author SUNNY
 * @Date 2025/11/8 21:09
 * @PackageName: io.sunny.platform.mcp.controller
 * @Description: 聊天控制器，处理AI聊天请求
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/chat")
public class ChatController {

    private final ChatClient chatClient;

    @Autowired
    public ChatController(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @PostMapping
    public ResponseEntity<ChatResponse> chat(@RequestBody ChatRequest request) {
        try {
            // 创建用户信息
            String userMessage = request.getMessage();

            // 使用流式Api调用聊天
            String content = chatClient.prompt()
                    .user(userMessage)
                    .call()
                    .content();

            return ResponseEntity.ok(new ChatResponse(content));
        } catch (Exception e) {
            // 移除printStackTrace，使用全局异常处理
            throw new RuntimeException("处理聊天请求失败", e);
        }
    }
}
